package com.sunchangpeng.jwtsample.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.MacSigner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by sunchangpeng
 */
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private MacSigner macSigner;

    public static class Authorize {
        private String token;

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }
    }

    @GetMapping("/authorize")
    public Authorize authorize(String userName, String password) {

        String token = JwtHelper.encode(userName, macSigner).getEncoded();

        Authorize authorize = new Authorize();
        authorize.setToken(token);
        return authorize;
    }
}
